<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page language="java" import="com.jy.domain.User"%>
<!DOCTYPE html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商家完善资料</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
  
    <link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
	<script src="${pageContext.request.contextPath}/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="${pageContext.request.contextPath}/plugins/bootstrap/js/bootstrap.min.js"></script>
   
   	<script>
		//使用ajax来判断旧密码与数据库中的密码是否一致
		$(function(){
			//先写一个鼠标移开的事件
			$('#oldpwd').blur(function(){
				var oldpwd = $('#oldpwd').val();//获取文本框的值
                var id = '<%=((User)session.getAttribute("user")).getId()%>';
				//部分路径
				var path = '<%=request.getContextPath()%>';
					$.ajax({
						type : "post",//post的提交方式
						url : path + "/user/findPassword",//完整的路径
						async : false,
						data : {//获取旧的密码和跳转到servlet文件的方法名
							"oldpwd" : oldpwd,
                            "id" : id
						},
						success : function(data) {//成功的话判断密码是否为数据库中的密码
							if (data == "true") {//如果为true则输入正确
								$('#oldpwd2').text('原密码输入正确');
                                $("#submit").attr("disabled",false);
							} else {
								$('#oldpwd2').text('原密码输入不正确');
                                $("#submit").attr("disabled",true);
								}
							},
						
						error : function() {
							alert("失败");
						}
					});
				})
				
				
				
			$('#pwd1').blur(function(){
				var pwd1 = $('#pwd1').val();
				if(pwd1==null || pwd1==""){
					$('#newpwd').text('新密码不能为空');
				}else{
					$('#newpwd').text('');
				}
			})
			
			
			
			$('#pwd2').blur(function(){
				var pwd1 = $('#pwd1').val();
				var pwd2 = $('#pwd2').val();
				if(pwd2 != pwd1){
					$('#newpwd2').text('两次输入的密码不一致');
				}else{
					$('#newpwd2').text('');
				}
				
			})
			
	})
</script>
   
    <script type="text/javascript">
    	function submit() {
    		var password = $('#pwd2').val();
			var path = '<%=request.getContextPath()%>';
			var id = '<%=((User)session.getAttribute("user")).getId()%>';
			$.ajax({
				type : "post",//post的提交方式
				url : path + "/user/updatePassword",//完整的路径
				async : false,
				data : {//获取旧的密码和跳转到servlet文件的方法名
					"id" : id,
					"password" : password
				},
				success : function(data) {//成功的话判断密码是否为数据库中的密码
					if (data == "true") {//如果为true则输入正确
						alert("密码修改成功");
						document.location="${pageContext.request.contextPath}";
					}
				},
			
			error : function() {
				alert("密码修改失败");
			}
		});
		}
    </script>
</head>

<body class="hold-transition skin-red sidebar-mini" >


		<form action="">
            <!-- 正文区域 -->
            <section class="content">

                <div class="box-body">

                    <!--tab页-->
                    <div class="nav-tabs-custom">

                        <!--tab头-->
                        <ul class="nav nav-tabs">
                       		
                            <li class="active">
                                <a href="#home" data-toggle="tab">修改密码</a>                             
                            </li>                            
                        </ul>
                        <!--tab头/-->
						
                        <!--tab内容-->
                        <div class="tab-content">
							
                            <!--表单内容-->
                            <div class="tab-pane active" id="home">
                                <div class="row data-type">     
                                
                                 <div class="col-md-2 title">原密码</div>
                                    <div class="col-md-10 data">
                                        <input id="oldpwd" type="text" style="width: 200px; height: 35px;"  placeholder="原密码" value="" required="required">
                                        <span id="oldpwd2" style="color: red;"></span>
                                    </div>
                                                                 
									<div class="col-md-2 title">新密码</div>
                                    <div class="col-md-10 data">
                                        <input type="text" id="pwd1" style="width: 200px; height: 35px;"  placeholder="新密码" value="" required="required">
										<span id="newpwd" style="color: red;"></span>				                                        
                                  </div>
                                    
                                    <div class="col-md-2 title">确认新密码</div>
                                    <div class="col-md-10 data">
                                        <input type="text" id="pwd2"  style="width: 200px; height: 35px;"   placeholder="确认新密码" value="" required="required">
                                        <span id="newpwd2" style="color: red;"></span>
                                    </div>
                                </div>
                            </div>
                           
                        </div>
                        <!--tab内容/-->
						<!--表单内容/-->
                    </div>
                 	
                   </div>
                  <div class="btn-toolbar list-toolbar">
				      <a data-toggle="modal" class="btn btn-danger" id="submit" onclick="submit()">提交</a>
				  </div>
			
            </section>
            <!-- 正文区域 /-->
			</form> 
       
</body>

</html>